package cn.iocoder.yudao.module.forward.controller.admin.datasourceregisterconfig;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.forward.controller.admin.datasourceregisterconfig.vo.*;
import cn.iocoder.yudao.module.forward.dal.dataobject.datasourceregisterconfig.DataSourceRegisterConfigDO;
import cn.iocoder.yudao.module.forward.service.datasourceregisterconfig.DataSourceRegisterConfigService;

@Tag(name = "管理后台 - 信息源注册配置信息")
@RestController
@RequestMapping("/forward/data-source-register-config")
@Validated
public class DataSourceRegisterConfigController {

    @Resource
    private DataSourceRegisterConfigService dataSourceRegisterConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建信息源注册配置信息")
    @PreAuthorize("@ss.hasPermission('forward:data-source-register-config:create')")
    public CommonResult<Integer> createDataSourceRegisterConfig(@Valid @RequestBody DataSourceRegisterConfigSaveReqVO createReqVO) {
        return success(dataSourceRegisterConfigService.createDataSourceRegisterConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新信息源注册配置信息")
    @PreAuthorize("@ss.hasPermission('forward:data-source-register-config:update')")
    public CommonResult<Boolean> updateDataSourceRegisterConfig(@Valid @RequestBody DataSourceRegisterConfigSaveReqVO updateReqVO) {
        dataSourceRegisterConfigService.updateDataSourceRegisterConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除信息源注册配置信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('forward:data-source-register-config:delete')")
    public CommonResult<Boolean> deleteDataSourceRegisterConfig(@RequestParam("id") Integer id) {
        dataSourceRegisterConfigService.deleteDataSourceRegisterConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得信息源注册配置信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('forward:data-source-register-config:query')")
    public CommonResult<DataSourceRegisterConfigRespVO> getDataSourceRegisterConfig(@RequestParam("id") Integer id) {
        DataSourceRegisterConfigDO dataSourceRegisterConfig = dataSourceRegisterConfigService.getDataSourceRegisterConfig(id);
        return success(BeanUtils.toBean(dataSourceRegisterConfig, DataSourceRegisterConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得信息源注册配置信息分页")
    @PreAuthorize("@ss.hasPermission('forward:data-source-register-config:query')")
    public CommonResult<PageResult<DataSourceRegisterConfigRespVO>> getDataSourceRegisterConfigPage(@Valid DataSourceRegisterConfigPageReqVO pageReqVO) {
        PageResult<DataSourceRegisterConfigDO> pageResult = dataSourceRegisterConfigService.getDataSourceRegisterConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DataSourceRegisterConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出信息源注册配置信息 Excel")
    @PreAuthorize("@ss.hasPermission('forward:data-source-register-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDataSourceRegisterConfigExcel(@Valid DataSourceRegisterConfigPageReqVO pageReqVO,
                                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DataSourceRegisterConfigDO> list = dataSourceRegisterConfigService.getDataSourceRegisterConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "信息源注册配置信息.xls", "数据", DataSourceRegisterConfigRespVO.class,
                BeanUtils.toBean(list, DataSourceRegisterConfigRespVO.class));
    }

    @GetMapping("/select")
    @Operation(summary = "获得信息源注册配置信息")
    @Parameter(name = "sourceId", description = "信息源ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('forward:data-source-register-config:query')")
    public CommonResult<DataSourceRegisterConfigRespVO> getDataSourceRegisterConfigBySourceId(@RequestParam("sourceId") Integer sourceId) {
        DataSourceRegisterConfigDO dataSourceRegisterConfig = dataSourceRegisterConfigService.getDataSourceRegisterConfigBySourceId(sourceId);
        if (dataSourceRegisterConfig == null) {
            return success(null);
        }
        return success(BeanUtils.toBean(dataSourceRegisterConfig, DataSourceRegisterConfigRespVO.class));
    }
}